﻿package com.thinktube
{
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.xml.*;
	import flash.events.*;

	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;//单选按钮的分组
	import fl.controls.ButtonLabelPlacement;
	import flash.display.Sprite;

	//字在按钮图标的左边还是右边或上下

	public class sectionBoard extends MovieClip
	{
		private var section_title:String;
		private var title_label:TextField = new TextField();
		private var sectionXml:XML;
		private var foo:Sprite = new Sprite();
		private var yOffset:uint = 40;
		private var lineHeight:uint = 20;
		public var answer:Object;
		

		public function sectionBoard(_xml:XML)
		{
			// constructor code
			sectionXml = _xml;
			section_title = sectionXml.attribute("title");

			this.title_label.text = section_title;
			this.title_label.x = 10;
			this.title_label.y = 20;
			this.title_label.width = 390;
			this.title_label.wordWrap = true;//允许换行


			var format1:TextFormat = new TextFormat();
			format1.font = "Lucida Grande";
			format1.size = 12;

			this.title_label.setTextFormat(format1);
			this.addChild(title_label);
			
			//drawItem(sectionXml);

			var ttS:Sprite = drawItem(sectionXml);
			this.addChild(ttS);

		}

		private function changeHandler(event:Event):void
		{
			var rbg:RadioButtonGroup = event.target as RadioButtonGroup;
			if (rbg.selectedData != null)
			{
				answer = rbg.selectedData;
				trace("answer"+answer);

			}
			else
			{
				trace("no value specified.");
			}
		}
		
		

		private function drawItem(sectionXml:XML):Sprite
		{
			var itemList:XMLList = sectionXml.item;
			var i:int = 0;
			var radioGroup:RadioButtonGroup = new RadioButtonGroup("group1");
			radioGroup.addEventListener(Event.CHANGE, changeHandler);

			for each (var item:XML in itemList)
			{
				var itemTitle:String = item.attribute("id") + item.text();
				var labelString:String = item.text();

				var itemRadioButton:quesRadioButton = new quesRadioButton();
				itemRadioButton.group = radioGroup;
				itemRadioButton.label = labelString;
				itemRadioButton.value = item.attribute("id");
				trace(labelString.length);
				if(labelString.length > 14 )
				{
					itemRadioButton.width = 400;
					itemRadioButton.y = lineHeight * i + yOffset;
					itemRadioButton.x = 0;
				}else{
					itemRadioButton.width = 200;
					itemRadioButton.y = lineHeight * Math.round( (i-1) / 2) + yOffset;
					itemRadioButton.x = itemRadioButton.width * Math.round(i % 2);

				}
				foo.addChild(itemRadioButton);
				i++;
			}
			return foo;
		}



	}

}